package com.sunflower.asone.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.sunflower.framework.application.BaseController;
import org.sunflower.framework.redis.RedisUtils;
import org.sunflower.framework.utils.StringUtils;

@Controller
@RequestMapping(value = "redis")
public class RedisTest extends BaseController{
	private final Logger logger = Logger.getLogger(RedisTest.class);

	@RequestMapping(value = "/test", method = { RequestMethod.GET })
	public void Test(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		/************************map 存储样例**********************/
		Map<String, String> rmap = new HashMap<String, String>();
		rmap.put("name", "fujianchao");
		rmap.put("password", "123");
		rmap.put("age", "12");
		saveCachedMap("myMap",rmap);
		
		System.out.println(getCachedMap("myMap"));
		/************************map 存储样例end**********************/
		
		/************************String 存储样例**********************/
		setcachedValue("myname","yetongxue");
		logger.info("-------------myname="+getCachedValue("myname"));
		/************************String 存储样例end**********************/
		
	}
	
	/**
	 * 存 Map
	 * @param cachedKey
	 * @return
	 */
	private void saveCachedMap(String cachedKey,Map<String, String> rmap){
		
		RedisUtils.setJedisMap(RedisUtils.SERVICE_REDIS_COMMON, cachedKey, rmap);
	}
	/**
	 * 取 Map
	 * @param cachedKey
	 * @return
	 */
	private Map<String, String> getCachedMap(String cachedKey){
		return RedisUtils.getJedisMap(RedisUtils.SERVICE_REDIS_COMMON, cachedKey);
	}
	
	/**
	 * 存储 String
	 * @param cachedKey
	 * @return
	 */
	private void setcachedValue(String redisKey,String redisValue)
	{
		redisKey = String.format("redisKey=[%s]", redisKey);
		//如果字典翻译值为空则不存入redis
		if(!StringUtils.isNullOrBlank(redisValue)){
			RedisUtils.setJedis(RedisUtils.SERVICE_REDIS_COMMON,redisKey, redisValue);
		}
	}
	/**
	 * 取 String
	 * @param cachedKey
	 * @return
	 */
	private String getCachedValue(String cachedKey)
	{
		cachedKey = String.format("redisKey=[%s]", cachedKey);
		//读取redis
		String cachedValue = RedisUtils.getJedis(RedisUtils.SERVICE_REDIS_COMMON,cachedKey);
		return cachedValue;
	}
}
